/**
 * Created by Freeman on 2016/6/28.
 */


import Vue from 'vue'
import Vuex from 'vuex'

import middlewares from './middlewares'

Vue.use(Vuex)


export const STORAGE_KEY = "todos-vuejs"

const state ={
    todos:JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]')
}

const mutations = {
    'ADD_TODO'(state,text){
        state.todos.push({
            text:text,
            done:false
        })
    },
    TOGGLE_TODO(state,todo){
        todo.done = !todo.done
    },
    DELETE_TODO(state,todo){
        state.todos.$remove(todo)
    },
    EDIT_TODO(state,todo,text){
        todo.text = text
    },
    TOGGLE_ALL(state,done){
        state.todos.forEach((todo)=>{
            todo.done=done
        })
    },
    CLEAR_COMPLETED(state){
        //过滤出done为false的，其余的过滤掉
        state.todos = state.todos.filter(todo=>!todo.done)
    }
}

export default new Vuex.Store({
    state,
    mutations,
    middlewares
})